using System.Collections;
using Platform;
using Platform.Skill.Core;
using Platform.Skill.Indicator;
using UnityEngine;
using static Entity;

[RequireComponent(typeof(SkillMgr))]
[RequireComponent(typeof(Player))]
public class SkillIndicatorMgr : MonoBehaviour
{
    private Player player;
    private SkillMgr skillMgr;
    private SkillData CurrentSkill
    {
        get { return skillMgr.currentSkill; }
        set { skillMgr.currentSkill = value; }
    }

    private BaseSkillIndicator skillIndicator;

    public bool isSkillChoose;

    void Awake()
    {
        player = GameMgr.Instance.player;
        skillMgr = GetComponent<SkillMgr>();
    }

    void OnEnable()
    {
        player.useSkillAction.performed += OnUseSkill;
        player.closeSkillAction.performed += OnCloseSkill;
    }

    void OnDisable()
    {
        player.useSkillAction.performed -= OnUseSkill;
        player.closeSkillAction.performed -= OnCloseSkill;
    }

    private void OnCloseSkill(UnityEngine.InputSystem.InputAction.CallbackContext context)
    {
        if (isSkillChoose)
        {
            skillMgr.entity.ChangeState(Entity.States.Idle);
            CloseSkillIndicator();
        }
    }

    /// <summary>
    /// 技能通过动画调用就不会走这里
    /// </summary>
    /// <param name="context"></param>
    private void OnUseSkill(UnityEngine.InputSystem.InputAction.CallbackContext context)
    {
        if (isSkillChoose)
        {
            if (CurrentSkill.stateNames.Length > 0)
            {
                StartCoroutine(ChangeStateAsync(CurrentSkill.stateNames));
            }
            else
                skillMgr.UseSkill(CurrentSkill.skillId);
        }

        IEnumerator ChangeStateAsync(States[] states)
        {
            foreach (var state in states)
            {
                yield return new WaitForEndOfFrame();
                skillMgr.entity.ChangeState(state);
            }
        }
    }

    #region  技能指示器
    public bool OpenSkillIndicator(SkillEnum skillEnum)
    {
        if (isSkillChoose)
            return false;
        var skill = skillMgr.PrepareSkill(skillEnum);
        CurrentSkill = skill;
        if (skill == null || skill.skillIndicator == SkillIndicatorEnum.None)
        {
            Debug.Log("不存在技能指示器");
            return false;
        }
        else
        {
            Debug.Log("打开技能指示器");
            isSkillChoose = true;
            SelectSpellIndicator(skill.skillIndicator);
            return true;
        }
    }

    public void CloseSkillIndicator()
    {
        isSkillChoose = false;
        CancelSpellIndicator();
    }

    // 此处耦合，考虑反射或映射
    private void SelectSpellIndicator(SkillIndicatorEnum skillIndicatorType)
    {
        switch (skillIndicatorType)
        {
            case SkillIndicatorEnum.Dotted:
                skillIndicator =
                    GetComponent<DottedIndicator>() ?? gameObject.AddComponent<DottedIndicator>();
                break;
            default:
                skillIndicator = null;
                break;
        }
        if (skillIndicator != null)
            StartCoroutine(ShowIndicator());

        IEnumerator ShowIndicator()
        {
            skillIndicator.skillData = CurrentSkill;
            skillIndicator.entity = skillMgr.entity;
            yield return null; // 等待下一帧
            skillIndicator.Show();
        }
    }

    private void CancelSpellIndicator()
    {
        skillIndicator.Hide();
    }

    #endregion
}
